package com.maserb.monitor.web.jw_our;

import com.maserb.monitor.common.AjaxResult;
import com.maserb.monitor.common.AjaxUtils;
import com.maserb.monitor.common.CreateTimeUtils;
import com.maserb.monitor.common.Func_T;
import com.maserb.monitor.entity.JwReport.JwFileInfoEntity;
import com.maserb.monitor.entity.jwOur.JwFilePhotoEntity;
import com.maserb.monitor.entity.jwOur.JwMessageEntity;
import com.maserb.monitor.model.JwFileInfoModel;
import com.maserb.monitor.service.JwReport.JwFileInfoService;
import com.maserb.monitor.service.jwOur.JwFilePhotoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

//附件上传接口
@Controller
@RequestMapping("/jwFilePhotoApp")
@Slf4j
public class JwFilePhotoAppController {
    @Autowired
    JwFilePhotoService jwFilePhotoService;

    //手机端上传文件到后台
    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult<JwFileInfoModel> uploadFile(MultipartHttpServletRequest muiltRequest) {
        return AjaxUtils.process(new Func_T<JwFileInfoModel>() {
            @Override
            public JwFileInfoModel invoke() throws Exception {
                String fileName = muiltRequest.getFileNames().next(); //得到文件名（注意。是content-type 中的name="file1"，而不是真正的文件名）
                MultipartFile file = muiltRequest.getFile(fileName);   //得到该文件
                //图片
                return jwFilePhotoService.uploadImage(file);
            }
        });
    }
    @RequestMapping("/saveFile")
    @ResponseBody
    public AjaxResult saveFile(String name ,String path){
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                JwFilePhotoEntity entity = new JwFilePhotoEntity();
                entity.setCreateTime(CreateTimeUtils.getCreateTime());
                entity.setName(name);
                entity.setPath(path);
                jwFilePhotoService.save(entity);
                return "success";
            }
        });
    }

}
